<?php
require_once dirname(__FILE__) . '/DbUtils.class.php';
require_once dirname(__FILE__) . '/metadata.php';
require_once dirname(__FILE__) . '/includes/MiscUtils.class.php';
require_once dirname(__FILE__) . '/AtdnUtils.php';
$myPdo = DbUtils::createPdoInst();
$start = date('Y-m-d', strtotime('next Monday', time()));
$classes = getClasses($myPdo);
foreach ($classes as $class) {
  //check if sent
  $cond_vals = new stdClass();
  $cond_vals->c = 't.class_id = :v1 AND (t.date >= :v2 AND t.date <= :v3)';
  $cond_vals->v = array(':v1' => $class->id, ':v2' => date('Y-m-d', strtotime('last monday', strtotime('tomorrow'))), ':v3' => date('Y-m-d', strtotime('last monday', strtotime('tomorrow'))+6*86400));
  $sents = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'attendences_logs', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($sents) > 0) {
    continue;
  }
  $path = class2Xls($myPdo,$class,$start,0); 
  xls2Email($path,$class->teacher_email);
  //add logs
  $obj = new stdClass();
  $obj->track_id = MiscUtils::unique();
  $obj->class_id = $class->id;
  $obj->start = $start;
  $obj->end = date('Y-m-d', strtotime('next Monday', time())+86400*6);
  $obj->date = date('Y-m-d', time());
  $obj->text = 'Roster is sent to ' .  $class->teacher_name;
  $obj->is_uploaded = 0;
  $obj->file = '';
  $obj->path = '';
  DbUtils::add($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'attendences_logs', $obj);
}
?>